// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {Utils} from "./utils";

const {ccclass, property} = cc._decorator;
declare const wx: any;
declare const UserGameData
@ccclass
export default class ShowReviveScore extends cc.Component {

  @property(cc.Node)
  content1: cc.Node = null;

  @property(cc.Label)
  labelTips: cc.Label = null;

  @property(cc.Label)
  labelName: cc.Label = null;

  @property(cc.Sprite)
  avatar: cc.Sprite = null;

  @property(cc.Label)
  labelScore: cc.Label = null

  @property(cc.Node)
  content2: cc.Node = null;

  @property(cc.Node)
  wxDataDispatcher: cc.Node = null;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    this.content2.active = false
    this.wxDataDispatcher.on('close', () => {
      this.content2.active = false
      this.content1.active = true
    })
    this.wxDataDispatcher.on('showNext', (e: any) => {
      const data = e.detail
      this.content1.active = false
      this.content2.active = true
      this.showNext(data.score)
    })
  }

  async showNext(score: number) {
    this.labelScore.string = ''
    this.labelName.string = ''
    this.labelTips.string = ''
    this.avatar.node.active = false;
    console.log('show')
    let next = await Utils.findNext(score);
    console.log('next')
    if (next) {
      this.labelTips.string = '下一个即将超越'
    } else {
      this.labelTips.string = '超越所有最佳战绩'
      next = {...await Utils.getUserInfo(), score} as any;
    }
    this.labelScore.string = next.score.toString()
    this.labelName.string = next.nickname
    this.avatar.spriteFrame = await Utils.loadRes(next.avatarUrl)
    this.avatar.node.active = true
  }

  start() {

  }

  // update (dt) {}
}
